﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aandelendelen.Mail;
using Aandelendelen.Caching;
using Aandelendelen.Obs;

namespace Aandelendelen.Controls
{
	public partial class TellAFriendControl : AuthenticationRequiredBaseUserControl
	{
		protected override void OnFirstHit(object sender, EventArgs e)
		{
			MailArguments mailArgs = MailArguments.MailArgs;
			mailArgs.Clear();

			mailArgs["{sender}"] = this.UserInfo.FullName;
			mailArgs["{username}"] = this.UserInfo.Gebruikersnaam;
			mailArgs["{aantal}"] = DataCache.ActiveUsers.ToString();
			mailArgs["{amount}"] = ObjectFormatter.GetCurrencyString(DataCache.InvestedAmount);

			EMail email = MailCache.GetMail("tellafriend");

			string body = email.SendBody;

			if (body.StartsWith("{from:"))
			{
				int index = body.IndexOf("}", 1);
				if (index > -1)
				{
					body = body.Substring(index + 1);
				}
			}

			this.lblEmailContent.Text = body;
		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			try
			{
				this.SendMails();

				this.btnSend.Visible = false;
				this.ErrorMessageControl11.Show();
				this.ErrorMessageControl11.Text = "De mails zijn verstuurd.";
			}
			catch (FormatException ex)
			{
				this.ErrorMessageControl11.Show();
				this.ErrorMessageControl11.Text = ex.Message;
			}
		}

		private void SendMails()
		{
			BulkEMail emails = new BulkEMail();

			EMail email1 = this.SendMail(this.Name1.Text, this.Email1.Text);
			EMail email2 = this.SendMail(this.Name2.Text, this.Email2.Text);
			EMail email3 = this.SendMail(this.Name3.Text, this.Email3.Text);
			EMail email4 = this.SendMail(this.Name4.Text, this.Email4.Text);

			if (email1 != null)
				emails.Add(email1);

			if (email2 != null)
				emails.Add(email2);

			if (email3 != null)
				emails.Add(email3);

			if (email4 != null)
				emails.Add(email4);

			if (this.ChckBxSendCopy.Checked)
			{
				BulkEMail emailsCC = new BulkEMail();

				foreach (EMail email in emails)
				{
					EMail emailCC = new EMail(email);
					emailCC.Recipients = this.UserInfo.Email;

					emailsCC.Add(emailCC);
				}

				foreach (EMail emailCCa in emailsCC)
					emails.Add(emailCCa);
			}

			emails.Send(true);
		}

		private EMail SendMail(string name, string emailAddress)
		{
			if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(emailAddress))
			{
				return null;
			}

			if (EMail.IsEmailAddressValid(emailAddress) == false)
			{
				throw new FormatException("'" + emailAddress + "' is geen geldig emailadres");
			}

			MailArguments mailArgs = MailArguments.MailArgs;
			mailArgs.Clear();

			mailArgs["{sender}"] = this.UserInfo.FullName;
			mailArgs["{naam}"] = name;
			mailArgs["{username}"] = this.UserInfo.Gebruikersnaam;
			mailArgs["{aantal}"] = DataCache.ActiveUsers.ToString();

			EMail email = MailCache.GetMail("tellafriend");

			email.Recipients = emailAddress;

			return email;
		}
	}
}